<?php
/**
 * Special route class for Drupal urls
 *
 */
class RM_Drupal_Controller_Router_Route implements RM_Controller_Router_Route_Interface {

  /**
   * Controller name
   *
   * @var string
   */
  protected $_controller = "";

  /**
   * Action name
   *
   * @var string
   */
  protected $_action = "";

  /**
   * Module name
   *
   * @var string
   */
  protected $_module = "";

  /**
   * Instantiates route based on passed Zend_Config structure
   *
   * @param Zend_Config $config Configuration object
   */
  public static function getInstance(Zend_Config $config) {
    $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
    return new self($config->route, $defs);
  }

  /**
   * Prepares the route for mapping.
   *
   * @param string $route Map used to match with later submitted URL path
   * @param array $defaults Defaults for map variables with keys as variable names
   */
  public function __construct($defaults = array()) {
    $this->_controller = $defaults['controller'];
    $this->_action = $defaults['action'];
    $this->_module = $defaults['module'];
  }

  public function match($path) {
    $chunks = explode('/', trim($path, '?'));
    $url = $chunks[count($chunks) - 1];
    $params = explode('&', $url);
    $return = array();
    foreach ($params as $paramsString) {
      			$chunks = explode('=', $paramsString);
            if (count($chunks) == 1) {
                $name = $chunks[0];
                $value = $chunks[0];
            } elseif (count($chunks) >= 2) {
                $name = $chunks[0];
                $value = $chunks[1];
            }            
            $return[$name] = $value;
    }

    unset($return['option']);
      $return['controller'] = isset($return['actions']) ? $return['actions'] : $this->_controller;
    $return['action'] = isset($return['controllers']) ? $return['controllers'] : $this->_action;
    $return['module'] = $this->_module;

    unset($return['actions']);
    unset($return['controllers']);

    $this->_controller = $return['controller'];
    $this->_action = $return['action'];
    return $return;
  }

  public function assemble($data = array(), $reset = false, $encode = false) {
      
    $temp = array();
    foreach ($data as $name => $value) {
      $temp[] = "$name=$value";
    }

    $url = null;
    if (count($temp) != 0 ) {
      $url .= '&'.implode('&', $temp);
    }
    return $url;
  }

  public function getAccembleURLJsCode() {
    if ($this->_module == 'admin') {
      $index = url('admin/resmania/view');
    } else {
      $index = url('user/resmania/view');
    }
        $command = "RM.Common.AssembleURL = function(json){
      var url = '".$index."&actions='+json.controller+'&controllers='+json.action;
        if (json.parameters) {
                for (var i = 0; i < json.parameters.length; i++){
                    url = url + '&' + json.parameters[i].name + '=' + json.parameters[i].value;
                }
              }
    return url;
    }";
    return $command;
  }
}